{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "0ba8729d",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "ename": "FileExistsError",
     "evalue": "[WinError 183] 当文件已存在时，无法创建该文件。: 'imagel2'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mFileExistsError\u001b[0m                           Traceback (most recent call last)",
      "\u001b[1;32mc:\\temp\\temp\\ipykernel_18460\\3913922486.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      5\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      6\u001b[0m \u001b[1;31m# 创建存储图片的文件夹\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 7\u001b[1;33m \u001b[0mos\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mmakedirs\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'imagel2'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      8\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      9\u001b[0m \u001b[1;31m# 给定爬取的网站\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mD:\\anaconda\\lib\\os.py\u001b[0m in \u001b[0;36mmakedirs\u001b[1;34m(name, mode, exist_ok)\u001b[0m\n\u001b[0;32m    223\u001b[0m             \u001b[1;32mreturn\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    224\u001b[0m     \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 225\u001b[1;33m         \u001b[0mmkdir\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mname\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mmode\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    226\u001b[0m     \u001b[1;32mexcept\u001b[0m \u001b[0mOSError\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    227\u001b[0m         \u001b[1;31m# Cannot rely on checking for EEXIST, since the operating system\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mFileExistsError\u001b[0m: [WinError 183] 当文件已存在时，无法创建该文件。: 'imagel2'"
     ]
    }
   ],
   "source": [
    "import os\n",
    "import requests\n",
    "from lxml import etree\n",
    "from time import sleep\n",
    "\n",
    "# 创建存储图片的文件夹\n",
    "os.makedirs('imagel2')\n",
    "\n",
    "# 给定爬取的网站\n",
    "base_url = \"https://pic.netbian.com/4kdongwu/\"\n",
    "# UA伪装\n",
    "headers = {\n",
    "    \"User-Agent\": \"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36\"\n",
    "}\n",
    "\n",
    "# 接收用户输入的页数\n",
    "num_pages = int(input(\"请输入要爬取的页数：\"))\n",
    "\n",
    "# 循环爬取多页\n",
    "for page in range(1, num_pages + 1):\n",
    "    # 构造当前页的URL\n",
    "    if page == 1:\n",
    "        url = base_url\n",
    "    else:\n",
    "        url = base_url + f'index_{page}.html'\n",
    "\n",
    "    # 使用requests.get方法获取服务器响应\n",
    "    response = requests.get(url=url, headers=headers)\n",
    "\n",
    "    # 设置编码\n",
    "    response.encoding = 'gbk'\n",
    "\n",
    "    tree = etree.HTML(response.text)\n",
    "\n",
    "    img_src = tree.xpath(\"//ul[@class='clearfix']/li/a/img/@src\")\n",
    "    img_name = tree.xpath(\"//ul[@class='clearfix']/li/a/b/text()\")\n",
    "\n",
    "    for i in range(len(img_src)):\n",
    "        src = \"https://pic.netbian.com\" + img_src[i]\n",
    "        name = f\"page_{page}_{i}_{img_name[i]}\"\n",
    "\n",
    "        resp = requests.get(src, headers=headers).content\n",
    "        sleep(1)\n",
    "\n",
    "        with open(\"./imagel2/\" + name + '.jpg', \"wb+\") as f:\n",
    "            f.write(resp)\n",
    "\n",
    "print(\"over!\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "669a559b",
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "开始的页码：1\n",
      "结束页码：3\n"
     ]
    },
    {
     "ename": "NameError",
     "evalue": "name 'ctreat_request' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32mc:\\temp\\temp\\ipykernel_18460\\3732302224.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m     14\u001b[0m     \u001b[1;32mfor\u001b[0m \u001b[0mpage\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mstart_page\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mend_page\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     15\u001b[0m         \u001b[1;31m#1请求对象的定制\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 16\u001b[1;33m         \u001b[0mresp1\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mctreat_request\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mpage\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     17\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     18\u001b[0m         \u001b[1;31m#2获取网页源码\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mNameError\u001b[0m: name 'ctreat_request' is not defined"
     ]
    }
   ],
   "source": [
    "import os\n",
    "import requests\n",
    "from lxml import etree\n",
    "from time import sleep\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    \n",
    "    os.mkdir(\"img5\")\n",
    "    \n",
    "    start_page = int(input(\"开始的页码：\"))\n",
    "    \n",
    "    end_page = int(input(\"结束页码：\"))\n",
    "    \n",
    "    for page in range(start_page,end_page):\n",
    "        #1请求对象的定制\n",
    "        resp1 = ctreat_request(page)\n",
    "        \n",
    "        #2获取网页源码\n",
    "        content = get_text(resp1)\n",
    "        \n",
    "        #3下载\n",
    "        down_load(content)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "668f9a32",
   "metadata": {},
   "outputs": [],
   "source": [
    "def creat_request(page):\n",
    "    if page == 1:\n",
    "        url = \"https://pic.netbian.com/4kdongwu/index.html\"\n",
    "    else:\n",
    "        url = \"https://pic.netbian.com/4kdongwu/index_\"+str(page)+\".html\"\n",
    "        \n",
    "    headers = {\n",
    "    \"User-Agent\": \"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36\"\n",
    "    }  \n",
    "    \n",
    "     # 使用requests.get方法获取服务器响应\n",
    "    resp1 = requests.get(url=url, headers=headers)\n",
    "    \n",
    "    resp1.encodingcoding = \"gbk\"\n",
    "    \n",
    "    return resp1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "d4b8abed",
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_text(resp1):\n",
    "    \n",
    "    page_text = resp1.text\n",
    "    \n",
    "    return page_text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "7afbb843",
   "metadata": {},
   "outputs": [],
   "source": [
    "def down_load(page_text):\n",
    "    tree = etree.HTML(page_text)\n",
    "    \n",
    "    img_src = tree.xpath(\"//ul[@class='clearfix']/li/a/img/@src\")\n",
    "    img_name = tree.xpath(\"//ul[@class='clearfix']/li/a/b\")\n",
    "\n",
    "    for src, name in zip(img_src, img_name):\n",
    "        img_url = \"https://pic.netbian.com\" + src\n",
    "        \n",
    "        resp = requests.get(img_url).content\n",
    "        \n",
    "        sleep(1)  \n",
    "        with open(f\"./imagel2/{name}.jpg\", \"wb\") as f:\n",
    "            f.write(resp)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c64f0d7b",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7a28ddf6",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "python3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
